﻿
const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub")
    .configureLogging(signalR.LogLevel.Information)
    .build();

connection.on("ReceiveMessage", (user, message) => {
    //const encodedMsg = user + "说：" + message;
    console.log(user);
    console.log(message);
    $.successLayer(message);
});

connection.on("ReceivePrivateMessage", (message) => {
    var hubMessage = JSON.parse(message);
    console.log(hubMessage);
    $.chatLayer(hubMessage);
});


function start() {
    try {
        connection.start().then(function () {
            //$.successLayer('HUB(消息推送)服务已连接');
        });
        console.log("connected");
    } catch (err) {
        console.log(err);
        setTimeout(() => start(), 10000);
    }
}

setTimeout(function () { start(); }, 500);


connection.onclose(function () {
    $.errorLayer('HUB服务断开,正在重连...');
    start();
});





//connection.start().catch(err => console.error(err.toString()));